package com.gxa.jd.controller;

import com.gxa.exception.CustomerException;
import com.gxa.jd.dto.WxLoginDto;
import com.gxa.jd.pojo.User;
import com.gxa.jd.service.UserService;
import com.gxa.entity.PageResult;
import com.gxa.entity.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;



    @RequestMapping(value = "/wx/login")
    @ApiOperation(value = "微信登录",notes = "微信登录",httpMethod = "POST")
    public Result wxLogin2( @RequestBody WxLoginDto WxLoginDto) throws CustomerException {
        Map<String, String> result = userService.wxLogin(WxLoginDto.getCode());
        return new Result(result);
    }

    @RequestMapping(value = "/query")
    @ApiOperation(value = "查询登录信息",notes = "查询登录信息",httpMethod = "POST")
    public Result query(@RequestParam String token) throws CustomerException {
        User user = userService.queryUserByToken(token);
        return new Result(user);
    }


    @GetMapping("/findAll")
    public List<User> findAll() {
        return userService.findAll();
    }

    @GetMapping("/findPage")
    public PageResult<User> findPage(int page, int size) {
        return userService.findPage(page, size);
    }

    @PostMapping("/findList")
    public List<User> findList(@RequestBody Map<String, Object> searchMap) {
        return userService.findList(searchMap);
    }

    @PostMapping("/findPage")
    public PageResult<User> findPage(@RequestBody Map<String, Object> searchMap, int page, int size) {
        return userService.findPage(searchMap, page, size);
    }

    @GetMapping("/findById")
    public User findById(Long id) {
        return userService.findById(id);
    }


    @PostMapping("/add")
    public Result add(@RequestBody User user) {
        userService.add(user);
        return new Result();
    }

    @PostMapping("/update")
    public Result update(@RequestBody User user) {
        userService.update(user);
        return new Result();
    }

    @GetMapping("/delete")
    public Result delete(Long id) {
        userService.delete(id);
        return new Result();
    }

}
